package com.tedu.egcqmvcboot.controller;

import com.tedu.egcqmvcboot.mapper.UserMapper;
import com.tedu.egcqmvcboot.pojo.dto.UserArticleDTO;
import com.tedu.egcqmvcboot.pojo.dto.UserLoginDTO;
import com.tedu.egcqmvcboot.pojo.entity.Article;
import com.tedu.egcqmvcboot.pojo.entity.User;
import com.tedu.egcqmvcboot.pojo.vo.UserVO;
import com.tedu.egcqmvcboot.result.JsonResult;
import com.tedu.egcqmvcboot.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/v1/users")
@Slf4j
@Api(tags="用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation(value="用户登录")
    @PostMapping("/login")
    public JsonResult login(@Validated @RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        System.out.println("开始处理用户登录...");
        UserVO userVO=userMapper.selectUser(userLoginDTO.getUsername(),userLoginDTO.getPassword());
        if (userVO!=null){
            if (userLoginDTO.getPassword().equals(userLoginDTO.getPassword())){
                session.setAttribute("user",userVO);
                return JsonResult.success(userVO);
            }else {
                return new JsonResult(Status.USERNAME_PASSWORD_ERROR);
            }
        }
        return new JsonResult(Status.USERNAME_PASSWORD_ERROR);
    }
    @ApiOperation(value = "发表文章功能")
    @PostMapping("/article")
    public JsonResult insertArticle(@Validated @RequestBody UserArticleDTO userArticleDTO,@ApiIgnore HttpSession session){
        log.info("开始处理发表文章");
        UserVO userVO=(UserVO) session.getAttribute("user");
//        if (userVO==null){
//            return new JsonResult(Status.NOT_LOGIN);
//        }
//        if (userArticleDTO.getTitle()==null){
//            return new JsonResult(Status.VALIDATE_ERROR);
//        }
        Article article=new Article();
        article.setContent(userArticleDTO.getContent());
        article.setTitle(userArticleDTO.getTitle());
        article.setUserId(userVO.getId());

        userMapper.insertArticle(article);
        return JsonResult.success();
    }
    @ApiOperation(value = "查询指定文章功能")
    @GetMapping("/selectByArticleId")
    @ApiImplicitParam(name = "id",value = "文章id",required = true,example = "1", dataType = "int")
    public JsonResult selectByArticleId(@Range(min = 1,max = 100,message="文章ID必须在1-100之间") @RequestParam Integer id){
        System.out.println("开始处理查询文章功能");
        if (id<0||id>100){
            return new JsonResult(Status.VALIDATE_ERROR);
        }
        return JsonResult.success(userMapper.selectByArticleId(id));
    }
}
